//    Copyright (C) Kherty.  All rights reserved.
using System.Collections.Generic;
using System.Diagnostics.Contracts;

namespace OpenLS.Spreadsheet.Collections
{
    class ThreeItemsDictionary<TValue>: IFrugalDictionary<TValue> where TValue: class
    {
        #region IFrugalDictionary<TValue> Members

        int IFrugalDictionary<TValue>.Count
        {
            get {
                int count = 0;
                if (FrugalHelper.IsSet(first.Key)) count++;
                if (FrugalHelper.IsSet(second.Key)) count++;
                if (FrugalHelper.IsSet(third.Key)) count++;
                return count;
            }
        }
        public bool ContainsKey(int key)
        {
            TValue value;
            IFrugalDictionary<TValue> dic = this;
            return dic.TryGetValue(key, out value);
        }



        bool IFrugalDictionary<TValue>.Remove(int key)
        {
            if (first.Key == key)
            {
                first = FrugalHelper.GetEmptyPair<TValue>();
                return true;
            }
            if (second.Key == key)
            {
                second = FrugalHelper.GetEmptyPair<TValue>();
                return true;
            }
            if (third.Key == key)
            {
                third = FrugalHelper.GetEmptyPair<TValue>();
                return true;
            }
            return false;
        }

        IEnumerator<int> IFrugalDictionary<TValue>.GetEnumerator()
        {
            if (FrugalHelper.IsSet(first.Key))
                yield return first.Key;
            if (FrugalHelper.IsSet(second.Key))
                yield return second.Key;
            if (FrugalHelper.IsSet(third.Key))
                yield return third.Key;
        }

        bool IFrugalDictionary<TValue>.TryGetValue(int key, out TValue value)
        {
            if (first.Key == key)
            {
                value = first.Value;
                return true;
            }
            if (second.Key == key)
            {
                value = second.Value;
                return true;
            }
            if (third.Key == key)
            {
                value = third.Value;
                return true;
            }
            value = default(TValue);
            return false;
        }

        IFrugalDictionary<TValue> IFrugalDictionary<TValue>.TryAddValue(int key, TValue value)
        {
#if DEBUG
            Contract.Requires(!this.ContainsKey(key));
#endif
            if (!FrugalHelper.IsSet(first.Key))
            {
                first = new KeyValuePair<int, TValue>(key, value);
                return null;
            }
            if (!FrugalHelper.IsSet(second.Key))
            {
                second = new KeyValuePair<int, TValue>(key, value);
                return null;
            }
            if (!FrugalHelper.IsSet(third.Key))
            {
                third = new KeyValuePair<int, TValue>(key, value);
                return null;
            }
            return new SparseArrayDictionary<TValue>();
        }
        KeyValuePair<int, TValue> first = FrugalHelper.GetEmptyPair<TValue>();
        KeyValuePair<int, TValue> second = FrugalHelper.GetEmptyPair<TValue>();
        KeyValuePair<int, TValue> third = FrugalHelper.GetEmptyPair<TValue>();

        #endregion
    }
}
